package com.shouxin.attendance.serialport;

import android.media.AudioManager;
import android.media.SoundPool;

import com.shouxin.attendance.App;
import com.shouxin.attendance.R;

import org.apache.log4j.Logger;

import java.util.HashMap;

/**
 * 声音辅助类
 * Created by ligang on 16/7/31.
 */
public final class SoundHelper {

	private static final Logger logger = Logger.getLogger(SoundHelper.class);

	private final HashMap<SoundType, Integer> sounds = new HashMap<>();

	private final SoundPool soundPool;

	private SoundHelper() {
		soundPool = new SoundPool(2, AudioManager.STREAM_MUSIC, 5);
	}

	public static SoundHelper getInstance() {
		return Instance.instance;
	}

	public void initSounds() {
		logger.debug("initSounds...");
		sounds.put(SoundType.PASS, soundPool.load(App.getApp(), R.raw.pass, 1));
		sounds.put(SoundType.ALARM, soundPool.load(App.getApp(), R.raw.alarm, 1));
	}

	public void playSound(SoundType type) {
		logger.debug("playSound type =>" + type);
		switch (type) {
			case PASS:
				soundPool.play(sounds.get(type), 1, 1, 10, 0, 1);
				break;
			case ALARM:
				soundPool.play(sounds.get(type), 1, 1, 10, 0, 1);
				break;
			default:
				break;
		}
	}

	public enum SoundType {
		PASS, ALARM
	}

	private static class Instance {

		static SoundHelper instance = new SoundHelper();
	}
}
